package com.dreamspacex.ligox.controller;

import com.dreamspacex.ligox.pojo.User;
import com.dreamspacex.ligox.service.IUserService;
import com.github.cage.Cage;
import com.github.cage.GCage;
import com.google.gson.Gson;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;


@RequestMapping(value = "user")
@Controller
public class UserController {

    @Resource
    private IUserService userService;


    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(HttpServletRequest request, HttpServletResponse response, Model model) throws IOException {
        User user = new User();
        byte gender = 0;

        user.setUsername(System.currentTimeMillis() + "");
        user.setMobile("13418672672");
        user.setPassword("123456");
        user.setRealname("tt2");

        user.setGender(gender);
        user.setCity("x");
        user.setProvince("广东");
        user.setCountry("cn");
        user.setNickname("tested");
        user.setAvatar("http://dreamspacex.com/dsx_banner05.jpg");
        user.setAvatarVerified(0);

        user.setRegDeviceModel("");

        user.setCreatedAt(new Date());
        user.setUpdatedAt(new Date());

        user.setLastLoginDeviceModel("");
        user.setLastLoginTime(new Date());

        int result = this.userService.addUser(user);
        if (result != 0) {
            Logger.getLogger(UserController.class).debug("添加成功");
        } else {
            Logger.getLogger(UserController.class).debug("添加失败");
        }

        int page = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("size"));

        List<User> users = this.userService.selectAll(page * pageSize, pageSize);
        model.addAttribute("users", users);
        model.addAttribute("user", user);

        return "user/test";
    }

    @RequestMapping(value = "/testJson", method = RequestMethod.GET)
    @ResponseBody
    public String testJson(HttpServletRequest request, Model model) {

        int page = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("size"));
        List<User> users = this.userService.selectAll(page * pageSize, pageSize);
        Gson gson = new Gson();
        String usersJsonStr = gson.toJson(users);
        String jsonString = "{'code':0,'data':" + usersJsonStr + ",'msg':''}";
        return jsonString;
    }


    @RequestMapping(value = "/testJson2", method = RequestMethod.GET)
    @ResponseBody
    public List<User> testJson2(HttpServletRequest request, Model model) {
        int page = Integer.parseInt(request.getParameter("page"));
        int pageSize = Integer.parseInt(request.getParameter("size"));
        List<User> users = this.userService.selectAll(page * pageSize, pageSize);
        
        return users;
    }


    @RequestMapping(value = "/testCaptcha", method = RequestMethod.GET)
    public void testCaptcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
        response.addHeader("Cache-Control", "post-check=0, pre-check=0");
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");

        Cage cage = new GCage();
        ServletOutputStream out = response.getOutputStream();
        cage.draw(cage.getTokenGenerator().next(), out);

        try {
            out.flush();
        } finally {
            out.close();
        }
    }
}
